package com.guli.gulimall.product.service.impl;

import com.guli.gulimall.product.dao.AttrDao;
import com.guli.gulimall.product.entity.AttrEntity;
import com.guli.gulimall.product.vo.AttrGroupVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guli.common.utils.PageUtils;
import com.guli.common.utils.Query;

import com.guli.gulimall.product.dao.AttrGroupDao;
import com.guli.gulimall.product.entity.AttrGroupEntity;
import com.guli.gulimall.product.service.AttrGroupService;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {
    @SuppressWarnings("all")
    @Autowired
    private AttrDao attrDao;
    @Override
    public PageUtils queryPage(Long cateId,Map<String, Object> params) {

        QueryWrapper<AttrGroupEntity> queryWrapper = new QueryWrapper<>();
        if(!cateId.equals(0L)){
            queryWrapper.eq("catelog_id",cateId);
        }
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public List<AttrGroupVo> withattrByCatelogId(Long catelogId) {
        QueryWrapper<AttrGroupEntity> w = new QueryWrapper<>();
        w.eq("catelog_id",catelogId);
        List<AttrGroupEntity> attrGroupEntities = baseMapper.selectList(w);
        List<AttrGroupVo> collect = attrGroupEntities.stream().map(item -> {
            QueryWrapper<AttrEntity> wrapper = new QueryWrapper<>();
            wrapper.eq("attr_type", 1);
            wrapper.eq("attr_group_id", item.getAttrGroupId());
            List<AttrEntity> attrs = attrDao.selectList(wrapper);
            AttrGroupVo attrGroupVo = new AttrGroupVo();
            BeanUtils.copyProperties(item, attrGroupVo);
            attrGroupVo.setAttrs(attrs);
            return attrGroupVo;
        }).collect(Collectors.toList());
        return collect;
    }

}